package net.mikoo.seals.api.model.use;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.utility.ApiConstants;
import net.mikoo.seals.common.enums.CensorStatusEnum;
import net.mikoo.seals.common.enums.UsedStatusEnum;
import net.mikoo.seals.model.DevicesUsed;
import net.mikoo.seals.model.Upfile;

public class UseRecordDetailResponse extends BaseResponse {

	private static final long serialVersionUID = -6267874883918355777L;

	private Long useId;
	
	private String useTime;
	
	private String address;
	
	private String sealName;
	
	private String devicesCode;
	
	private String cause;
	
	private String status;
	
	private String statusName;
	
	private PersonInfo personInfo;
	
	private List<String> applyVoucherList;
	
	private List<String> voucherList;
	
	private String censorStatus;
	
	private String censorStatusName;
	
	private String censorPartyName;
	
	private String censorTime;
	
	public Long getUseId() {
		return useId;
	}

	public void setUseId(Long useId) {
		this.useId = useId;
	}

	public String getUseTime() {
		return useTime;
	}

	public void setUseTime(String useTime) {
		this.useTime = useTime;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getSealName() {
		return sealName;
	}

	public void setSealName(String sealName) {
		this.sealName = sealName;
	}

	public String getDevicesCode() {
		return devicesCode;
	}

	public void setDevicesCode(String devicesCode) {
		this.devicesCode = devicesCode;
	}

	public String getCause() {
		return cause;
	}

	public void setCause(String cause) {
		this.cause = cause;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatusName() {
		return statusName;
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}

	public PersonInfo getPersonInfo() {
		return personInfo;
	}

	public void setPersonInfo(PersonInfo personInfo) {
		this.personInfo = personInfo;
	}

	public List<String> getVoucherList() {
		return voucherList;
	}

	public void setVoucherList(List<String> voucherList) {
		this.voucherList = voucherList;
	}
	
	public List<String> getApplyVoucherList() {
		return applyVoucherList;
	}

	public void setApplyVoucherList(List<String> applyVoucherList) {
		this.applyVoucherList = applyVoucherList;
	}

	public String getCensorStatus() {
		return censorStatus;
	}

	public void setCensorStatus(String censorStatus) {
		this.censorStatus = censorStatus;
	}

	public String getCensorStatusName() {
		return censorStatusName;
	}

	public void setCensorStatusName(String censorStatusName) {
		this.censorStatusName = censorStatusName;
	}

	public String getCensorPartyName() {
		return censorPartyName;
	}

	public void setCensorPartyName(String censorPartyName) {
		this.censorPartyName = censorPartyName;
	}

	public String getCensorTime() {
		return censorTime;
	}

	public void setCensorTime(String censorTime) {
		this.censorTime = censorTime;
	}

	public static UseRecordDetailResponse valueOf(DevicesUsed devicesUsed) {
		UseRecordDetailResponse result = new UseRecordDetailResponse();
		
		if(devicesUsed == null) {
			return result;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		result.setUseId(devicesUsed.getDevicesUsedId());
		if(devicesUsed.getUsedTime() != null) {
			result.setUseTime(sdf.format(devicesUsed.getUsedTime()));
		}
		
		result.setAddress("");
		if(devicesUsed.getApply() != null) {
			result.setCause(devicesUsed.getApply().getCause());
		}
		
		if(devicesUsed.getDevices() != null) {
			result.setDevicesCode(devicesUsed.getDevices().getDevicesCode());
		}
		
		if(devicesUsed.getSeal() != null) {
			result.setSealName(devicesUsed.getSeal().getSealName());
		}
		
		UsedStatusEnum status = UsedStatusEnum.getByCode(devicesUsed.getUsedStatus());
		if(status != null) {
			result.setStatus(devicesUsed.getUsedStatus());
			result.setStatusName(status.getMessage());
		}
		
		if(devicesUsed.getPerson() != null) {
			PersonInfo personInfo = new PersonInfo();
			
			personInfo.setName(devicesUsed.getPerson().getName());
			if(StringUtils.isNotBlank(devicesUsed.getPerson().getPortrait())) {
				personInfo.setPortrait(devicesUsed.getPerson().getPortrait());
			}
			personInfo.setPosition(devicesUsed.getPerson().getPosition());
			
			result.setPersonInfo(personInfo);
		}
		
		if(devicesUsed.getUpfileList() != null) {
			List<String> vouchers = new ArrayList<String>();
			for(Upfile upfile : devicesUsed.getUpfileList()) {
				if(upfile == null || StringUtils.isBlank(upfile.getFileUrl())) {
					continue;
				}
				vouchers.add(upfile.getFileUrl());
			}
			result.setVoucherList(vouchers);
		}
		
		if(devicesUsed.getApply() != null && devicesUsed.getApply().getUpfiles() != null) {
			result.setApplyVoucherList(devicesUsed.getApply().getUpfiles().stream().map(item -> item.getFileUrl()).collect(Collectors.toList()));
		}
		
		CensorStatusEnum censorStatus = CensorStatusEnum.getByCode(devicesUsed.getCensorStatus());
		if(censorStatus != null) {
			result.setCensorStatus(censorStatus.getCode());
			result.setCensorStatusName(censorStatus.getMessage());
			result.setCensorPartyName(devicesUsed.getCensorPartyName());
		}
		
		return result;
	}

	private static class PersonInfo{
		
		private String name;
		
		private String portrait;
		
		private String position;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPortrait() {
			return portrait;
		}

		public void setPortrait(String portrait) {
			this.portrait = portrait;
		}

		public String getPosition() {
			return position;
		}

		public void setPosition(String position) {
			this.position = position;
		}
	}
}
